package com.project.plugin.util;

import com.alibaba.fastjson.JSON;
import com.project.plugin.constants.DingTalkRobotConstants;
import com.project.plugin.dto.LinkMessage;
import com.project.plugin.dto.MarkdownMessage;
import com.project.plugin.dto.TestMessage;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * PACKAGE      :  com.project.plugin.util
 * CREATE DATE  :  2018/6/9
 * AUTHOR       :  xiaoyi.xie
 * 文件描述      :
 */
public class NotifyUtil {

    /**
     * 发送普通文本消息
     *
     * @param accessToken 钉钉群组的机器人的Hook地址的AccessToken
     * @param content     消息内容
     * @param atMobiles   被@人的手机号
     */
    public static void sendTestMessage(String accessToken, String content, String[] atMobiles) {
        TestMessage testMessage = new TestMessage(content, atMobiles);

        sendPost(DingTalkRobotConstants.DING_TALK_ROBOT_WEB_HOOK + accessToken, JSON.toJSONString(testMessage));
    }

    /**
     * 发送link类型消息
     *
     * @param accessToken 钉钉群组的机器人的Hook地址的AccessToken
     * @param title       消息标题
     * @param text        消息内容。如果太长只会部分展示
     * @param picUrl      图片URL
     * @param messageUrl  点击消息跳转的URL
     */
    public static void sendLinkMessage(String accessToken, String title, String text, String picUrl, String messageUrl) {
        LinkMessage linkMessage = new LinkMessage(title, text, picUrl, messageUrl);

        sendPost(DingTalkRobotConstants.DING_TALK_ROBOT_WEB_HOOK + accessToken, JSON.toJSONString(linkMessage));
    }

    /**
     * 发送markdown类型消息
     *
     * @param accessToken 钉钉群组的机器人的Hook地址的AccessToken
     * @param title       首屏会话透出的展示内容
     * @param text        markdown格式的消息
     * @param atMobiles   被@人的手机号(在text内容里要有@手机号)
     */
    public static void sendMarkdownMessage(String accessToken, String title, String text, String[] atMobiles) {
        MarkdownMessage markdownMessage = new MarkdownMessage(title, text, atMobiles);

        sendPost(DingTalkRobotConstants.DING_TALK_ROBOT_WEB_HOOK + accessToken, JSON.toJSONString(markdownMessage));
    }

    private static void sendPost(String robotWebHook, String message) {
        try {
            HttpClient httpclient = HttpClients.createDefault();

            HttpPost httppost = new HttpPost(robotWebHook);
            httppost.addHeader("Content-Type", "application/json; charset=utf-8");

            StringEntity se = new StringEntity(message, "utf-8");
            httppost.setEntity(se);

            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(response.getEntity(), "utf-8");
                System.out.println(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}